<template>
  <div>
    <el-table :data="List">
      <el-table-column prop="_id" label="标题" width="140"></el-table-column>
      <el-table-column
        prop="tags[0].name"
        label="上级元素"
        width="140"
      ></el-table-column>
      <el-table-column prop="title" label="标题" width="140"></el-table-column>
      <!-- <el-table-column prop="body" label="内容" width="120"></el-table-column> -->
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button
            @click="$router.push(`/articles/edit/${scope.row._id}`)"
            type="text"
            size="small"
            >编辑</el-button
          >
          <el-button @click="remove(scope.row)" type="text" size="small"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      List: [],
    };
  },
  methods: {
    // 获取文章列表
    async getList() {
      const res = await this.$http.get("/articles");
      this.List = res.data;
      console.log(this.List);
    },
    // 删除文章
    remove(row) {
      this.$confirm(`是否删除文章 '${row.title}'`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        const res = await this.$http.delete(`/articles/${row._id}`);
        this.$message({
          type: "success",
          message: "删除成功!",
        });
        this.getList();
      });
    },
  },
  created() {
    this.getList();
  },
};
</script>

<style>
</style>